#!/bin/sh
#
#  This file is part of systemd.
#
#  Copyright (C) 2013 Jolla Ltd.
#  Contact: Pekka Lundstrom  <pekka.lundstrom@jolla.com>
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
#  systemd is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with systemd; If not, see <http://www.gnu.org/licenses/>.
#  ------------------------
#
# This is helper script that can be used to run systemctl commands on user 
# session when logged in as root
# It scans all running sessions and runs the command for each user
# Usage:
# systemctl-user <any systemctl command with parameters>
# example:
# systemctl-user stop myapp.service

# First check that we are running in env where this works 
# (rpm build envs don't have all tools)
which pidof &> /dev/null && which cut &> /dev/null && which ps &> /dev/null || exit 1
# Get all running systemd user sessions
SESSION_PIDS=$(pidof systemd | cut -sd " " -f2-)
[ -z "$SESSION_PIDS" ] && echo "systemd user session is not running" && exit 1
MY_ARGS=$@
for PID in $SESSION_PIDS; do
    U_NAME=$(ps -o user= $PID)
    #echo "$U_NAME: systemctl --user $MY_ARGS"
    su -l $U_NAME -c "systemctl --user $MY_ARGS"
done


